package com.wdd.studentmanager.config;

import com.wdd.studentmanager.util.UploadUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.FileNotFoundException;

/**
 * 上传文件，保存图片到本地文件夹
 * 在springboot中，上传图片到静态资源里并不能立即显示该图片，需要进行热部署(重启)之后才能实现该功能
 * @author yangj
 * @create 2021-03-02 15:47
 */
@Configuration
/*springbooot 2.0 使用*/
public class UploadConfig implements WebMvcConfigurer{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //映射图片保存地址
        //file: 项目路径/src/main/resources/static/upload/imgs/
        String filePath = UploadUtil.getJarRootPath() + "/src/main/resources/static/upload/imgs/";
        System.out.println(filePath);
        registry.addResourceHandler("/upload/imgs/**").addResourceLocations("file:"+ filePath);
    }
}

/*  spring boot 1.5
public class UploadConfig extends WebMvcConfigurerAdapter  {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //映射图片保存地址
        //file: 项目路径\\src\\main\\resources\\static\\upload\\
        String filePath = UploadUtil.getJarRootPath() + "/src/main/resources/static/upload/imgs/";
        System.out.println(filePath);
        registry.addResourceHandler("/upload/imgs/**").addResourceLocations("file:"+ filePath);

    }
}*/
